<?php
class BugController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function bugAction()
    {
        // action body
    }
    
    public function createAction() {
    	
    }
    
    public function submitAction() {
    	$form = new Form_BugReportForm();
    	$form->setAction('/bug/submit');
    	$form->setMethod('post');
    	if($this->getRequest()->isPost()) {
    		if($form->isValid($_POST)) {
    			$bugModel = new Model_Bug();
    			$res = $bugModel->createBug(
    				$form->getValue('author'),
    				$form->getValue('email'),
    				$form->getValue('date'),
    				$form->getValue('url'),
    				$form->getValue('description'),
    				$form->getValue('priority'),
    				$form->getValue('status')
    			);
    			
    			if( $res ) {
    				$this->_forward('confirm');
    			} else {
    				echo "some error: " . $res;
    			}
    			
    		}
    	}
    	$this->view->form = $form;
    }
    
    public function confirmAction(){
    	
    }
    
    public function listAction(){

    	
    	//get the filter form
    	$form = new Form_BugReportListToolsForm();
    	$form->setAction('/bug/list');
    	$form->setMethod('post');
    	$this->view->listToolsForm = $form;
    	
    	//set the default values to null
    	//$sort = null;
    	//$filter = null;
    	
    	// set the sort and filter criteria. yoi neet to update this to use the request,
    	// as these values can come in from the form post or a url parameter
    	$sort = $this->_request->getParam('sort', null);
    	$filterField = $this->_request->getParam('filter_field', null);
    	$filterValue = $this->_request->getParam('filter');
    	
    	if( !empty($filterField)) {
    		$filter[$filterField] = $filterValue;
    	} else {
    		$filter = null;
    	}
    	
    	// now you need to manually set these controls values
    	$form->getElement('sort')->setValue($sort);
    	$form->getElement('filter_field')->setValue($filterField);
    	$form->getElement('filter')->setValue($filterValue);
    	
    	// fetch the bug paginator adapter
    	$bugModel = new Model_Bug();
    	$adapter = $bugModel->fetchPaginatorAdapter( $filter, $sort);
    	$paginator = new Zend_Paginator($adapter);

    	//show 10 bugs per page
    	$paginator->setItemCountPerPage(10);
    	
    	// get the page number that is passed in the request.
    	// if none is set then default to page 1.
    	$page = $this->_request->getParam('page', 1 );
    	$paginator->setCurrentPageNumber($page);
    	
    	// pass the paginator to the view to rende
    	$this->view->paginator = $paginator;
    	
    	/*
    	// if this request is a postback and is valid, then add the sort filter criteria
    	if( $this->getRequest()->isPost()){
    		if($form->isValid($_POST)){
    			$sortValue = $form->getValue('sort');
    			if($sortValue != '0'){
    				$sort = $sortValue;
    			}
    			
    			$filterFieldValue = $form->getValue('filter_field');
    			if($filterFieldValue != '0'){
    				$filter[$filterFieldValue] = $form->getValue('filter');
    			}
    		}
    	}
    	
    	// fetch the curretn bugs
    	$bugModel = new Model_Bug();
    	$this->view->bugs = $bugModel->fetchBugs( $filter, $sort);
  		*/
    }
    
    public function editAction() {
    	date_default_timezone_set("Europe/Berlin");
    	$bugModel = new Model_Bug();
    	$form = new Form_BugReportForm();
    	$form->setAction('/bug/edit');
    	$form->setMethod('post');
    	
    	if( $this->getRequest()->isPost()) {
    		if($form->isValid($_POST)) {
    			
    			$res = $bugModel->updateBug(
    				$form->getValue('id'),
    				$form->getValue('author'),
    				$form->getValue('email'),
    				$form->getValue('date'),
    				$form->getValue('url'),
    				$form->getValue('description'),
    				$form->getValue('priority'),
    				$form->getValue('status')
    			);
    			return $this->_forward('list');
    		}
    	} else {
    		$id = $this->_request->getParam('id');
    		$bug = $bugModel->find($id)->current();
    		$form->populate( $bug->toArray() );
    		$form->getElement('date')->setValue(date('m-d-Y', $bug->date));
    	}
    	//$this->view->bug = $bug;
    	$this->view->form = $form;    	
    }

    public function deleteAction() {
    	$bugModel = new Model_Bug();
    	$id = $this->_request->getParam('id');
    	$bugModel->deleteBug($id);
    	return $this->_forward('list');
    }


}